package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Rule;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.PlantService;
import com.jgsu.farm.service.RuleService;
import com.jgsu.farm.service.StressCategoryService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.FileUploadUtil;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Api(value = "farm/sysuser/rule", tags = "诊断规则管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/rule")
public class RuleController {

    @Autowired
    RuleService ruleService;
    @Autowired
    PlantService plantService;
    @Autowired
    StressCategoryService stressCategoryService;
    @Autowired
    FileUploadUtil fileUploadUtil;


    @ApiOperation(value = "诊断规则添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult ruleAdd(String rulename, Integer author, Integer stresstype, Integer cropid,
                                MultipartFile rulefile, String description) throws IOException {
        String uploadFile = fileUploadUtil.upload(rulefile);
        Rule rule = new Rule(rulename, author, stresstype, cropid,
                uploadFile, description);
        try {
            ruleService.add(rule);
            log.info("诊断规则添加成功，诊断规则名字为：{}", rule.getRulename());
            return CommonResult.success(rule);
        } catch (DataAddException e) {
            log.error("诊断规则添加失败，诊断规则名字为：{}", rule.getRulename());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "诊断规则删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult ruleDelete(@RequestParam("ruleid") Integer ruleid) {
        try {
            ruleService.delete(ruleid);
            log.info("删除id为：{}的诊断规则信息",ruleid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException d) {
            log.error("不存在id为：{}的诊断规则信息，删除失败",ruleid);
            throw new DataNotExistException(d.getCode(), d.getMessage());
        }
    }

    /**
     * 诊断规则查询功能
     */
    @ApiOperation(value = "诊断规则查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Rule>> ruleList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                 @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Rule> rules = ruleService.selectAll(page, pagesize);
            log.info("查询所有的诊断规则信息");
            return CommonResult.success(rules);
        } catch (DataNotExistException nd) {
            log.error("诊断规则信息表中没有数据");
            throw new DataNotExistException(nd.getCode(), nd.getMessage());
        }
    }

    /**
     * 诊断规则修改
     */
    @ApiOperation(value = "诊断规则修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult ruleUpdate(Integer ruleid,String rulename, Integer author, Integer stresstype, Integer cropid,
                                   MultipartFile rulefile, String description) throws IOException {
        String uploadFile = fileUploadUtil.upload(rulefile);
        Rule rule = new Rule(ruleid,rulename, author, stresstype, cropid,
                uploadFile, description);
        try {
            ruleService.update(rule);
            log.info("更新诊断标题为{}的诊断规则信息成功", rule.getRulename());
            return CommonResult.success(rule);
        } catch (DataNotExistException e) {
            log.error("更新诊断标题为{}的诊断规则信息失败", rule.getRulename());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
